/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.entity;

import com.inspur.edp.cef.api.attr.ChildAttribute;
import com.inspur.edp.cef.designtime.api.IGspCommonDataType;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.entity.accessor.entity.AccessorCollection;
import com.inspur.edp.cef.gencommon.CefPropertyInfo;
import com.inspur.edp.cef.gencommon.ReflectionUtils;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.coreGenerator.base.accessorgen.method.base.AcsCopyGenerator;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.DataTypeImpContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityGeneratorContext;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.List;
import org.eclipse.jdt.core.dom.*;

public class EntityAcsCopyGenerator extends AcsCopyGenerator
{
	private java.util.Map<String, String> childAndFieldNames;

	public EntityAcsCopyGenerator(String className, String nodeCode, List<MediatePropertyInfo> dataType, java.util.Map<String, String> childAndFieldNames,
			EntityGeneratorContext entityGeneratorContext) {
		super(className, nodeCode, dataType,entityGeneratorContext);
		this.childAndFieldNames = childAndFieldNames;
	}

	@Override
	protected boolean isExtendPro(IGspCommonField field) {
		return false;
	}

	@Override
	protected void addExtendPropertyStatements(Block block) {
		super.addExtendPropertyStatements(block);
		if(dataType.isEmpty())
			return;
		for (MediatePropertyInfo child:dataType) {
			if(child.getNode() != null) {
				addChildObjectPropertyStatements(child.getNode(), block);
			}
		}
	}

	private void addChildObjectPropertyStatements(IGspCommonObject child, Block block) {
		IfStatement ifStatement=ast.newIfStatement();
		String fieldName=childAndFieldNames.get(child.getCode());
		setChildIfExpression(fieldName,ifStatement);
		setChildIfThen(fieldName,ifStatement);
		block.statements().add(ifStatement);
	}


	private void setChildIfExpression( String fieldName, IfStatement ifStatement)
	{
		InfixExpression infixExpression =ast.newInfixExpression();
		infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);
		infixExpression.setLeftOperand(ast.newSimpleName(fieldName));
		infixExpression.setRightOperand(ast.newNullLiteral());
		ifStatement.setExpression(infixExpression);
	}

	private void setChildIfThen(String fieldName, IfStatement ifStatement)
	{
		Block block=ast.newBlock();
		Assignment assignment=ast.newAssignment();
		assignment.setLeftHandSide(ast.newQualifiedName(ast.newSimpleName(varResult),ast.newSimpleName(fieldName)));
		assignment.setOperator(Assignment.Operator.ASSIGN);

		MethodInvocation methodInvocation=ast.newMethodInvocation();
		methodInvocation.setName(ast.newSimpleName("copy"));
		methodInvocation.arguments().add(ast.newSimpleName(varResult));
		ParenthesizedExpression parenthesizedExpression = ast.newParenthesizedExpression();
		CastExpression castExpression = ast.newCastExpression();
		castExpression.setType(ast.newSimpleType(ast.newName(AccessorCollection.class.getTypeName())));
		castExpression.setExpression(ast.newSimpleName(fieldName));
		parenthesizedExpression.setExpression(castExpression);
		methodInvocation.setExpression(parenthesizedExpression);

		assignment.setRightHandSide(methodInvocation);

		ExpressionStatement expressionStatement=ast.newExpressionStatement(assignment);
		block.statements().add(expressionStatement);
		ifStatement.setThenStatement(block);
	}
}
